package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.DownloadInfo;
import com.example.demo.entity.FileInfo;
import com.example.demo.entity.UploadAddress;
import com.example.demo.entity.UploadComplete;
import com.example.demo.entity.UploadPrepare;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.UserPermission;
import com.example.demo.entity.WebOfficeResponse;
import com.example.demo.service.UserService;
import com.example.demo.service.WebOfficeEditService;
import com.example.demo.service.WebOfficePreview;


@RestController
public class WebOfficeController {
    @Autowired
    private WebOfficeEditService editService;

    @Autowired
    private WebOfficePreview previewService;

    /*
     * Weboffice文档预览 -> 详见： <br>
     */


     /**
     * 获取文件信息
     *
     * @param fileId 文件id <br>
     *               <a href="https://solution.wps.cn/docs/callback/preview.html#获取文件信息">-详见官方文档-</a>
     */
    @GetMapping("/v3/3rd/files/{file_id}")
    public WebOfficeResponse<FileInfo> GetFileInfo(@PathVariable("file_id") final String fileId) {
        return WebOfficeResponse.ok(previewService.GetFileInfo(fileId));
    }

    /**
     * 获取下载信息
     *
     * @param fileId 文件id <br>
     *               <a href="https://solution.wps.cn/docs/callback/preview.html#获取文件下载地址">-详见官方文档-</a>
     */
    @GetMapping("/v3/3rd/files/{file_id}/download")
    public WebOfficeResponse<DownloadInfo> GetDownloadInfo(@PathVariable("file_id") final String fileId) {
        return WebOfficeResponse.ok(previewService.GetDownloadInfo(fileId));
    }

    /**
     * 获取文档用户权限
     *
     * @param fileId 文件id <br>
     *               <a href="https://solution.wps.cn/docs/callback/preview.html#文档用户权限">-详见官方文档-</a>
     */
    @GetMapping("/v3/3rd/files/{file_id}/permission")
    public WebOfficeResponse<UserPermission> GetUserPermission(@PathVariable("file_id") final String fileId) {
//        return WebOfficeResponse.ok(previewService.GetUserPermission(fileId));
        return null;
    }


     /*
     * Weboffice文档编辑 -> 详见： <br>
     */

     /**
     * 三阶段保存 - 准备上传阶段
     *
     * @param fileId 文件id <br>
     *               <a href = "https://solution.wps.cn/docs/callback/save.html#准备上传阶段">-详见官方文档-</a>
     */
    @GetMapping("/v3/3rd/files/{file_id}/upload/prepare")
    public WebOfficeResponse<UploadPrepare> uploadPrepare(@PathVariable("file_id") String fileId) {
        return WebOfficeResponse.ok(editService.UploadPrepare(fileId));
    }

    /**
     * 三阶段保存 - 获取上传地址
     *
     * @param fileId  文件id
     * @param request 上传的文件 <br>
     *                <a href = "https://solution.wps.cn/docs/callback/save.html#获取上传地址">-详见官方文档-</a>
     */
    @PostMapping("/v3/3rd/files/{file_id}/upload/address")
    public WebOfficeResponse<UploadAddress.Response> uploadAddress(@PathVariable("file_id") String fileId,
                                                                                                 @RequestBody UploadAddress.Request request) {
        request.setFileId(fileId);
        return  WebOfficeResponse.ok(editService.uploadAddress(request));
    }

    /**
     * 三阶段保存 - 上传完成后，回调通知上传结果
     *
     * @param fileId  文件id
     * @param request 上传的文件 <br>
     *                <a href = "https://solution.wps.cn/docs/callback/save.html#上传完成后，回调通知上传结果">-详见官方文档-</a>
     */
    @PostMapping("/v3/3rd/files/{file_id}/upload/complete")
    public WebOfficeResponse<FileInfo> uploadComplete(@PathVariable("file_id") String fileId,
                                                           @RequestBody UploadComplete.Request request) {
        request.setFileId(fileId);
        return WebOfficeResponse.ok(editService.uploadComplete(request));
    }


     /**
     * 获取用户信息
     *
     * @param userIds 用户id列表 <br>
     *               <a href = "https://solution.wps.cn/docs/callback/user.html#用户信息">-详见官方文档-</a>
     */
    @GetMapping("/v3/3rd/users")
    public WebOfficeResponse<List<UserInfo>> getUserInfo(@RequestParam("user_ids") List<String> userIds)  {
        return WebOfficeResponse.ok(new UserService().getUserInfo(userIds));
    }
}

